package com.atao.view;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;
/**程序都是执行函数*/
public class MyViewSwitcher extends ViewSwitcher {
	private Context context;
	/**有多少内容要显示*/
	private int contentCount;
	/**动画切换的间�*/
	private int timeDistace;
	private Handler handler ;
	Map<Integer,View> recordViews;
	public MyViewSwitcher(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context=context;
		handler = new Handler();
		recordViews=new HashMap<Integer,View>();
	}
	/**初始化MyViewSwitcher和设置切换动**/
	public void initView(int contentCount,int timeDistace){
		setNextViewAnimation();
		this.contentCount=contentCount;
		this.timeDistace=timeDistace;
		View view1= View.inflate(getContext(), R.layout.loading_view, null);
		view1.setTag(0);
		this.addView(view1);//0
		View view2= View.inflate(getContext(), R.layout.loading_view, null);
		view2.setTag(1);
		this.addView(view2);//1
		startDisplay();
	}
	boolean first2End=true;
	boolean end2First=false;
	boolean judgeFirstAndEnd=first2End;
	/**判断图片是开始显示还是显示结束了*/
	int flag=0;
	private void startDisplay(){
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				View viewSwitchContent=recordViews.get(flag);
				Integer pos;
				if(viewSwitchContent!=null){
					pos=Integer.valueOf(viewSwitchContent.getTag().toString());
				}else{
					Object tag=MyViewSwitcher.this.getCurrentView().getTag();
					pos=Integer.valueOf(tag.toString());
					viewSwitchContent=View.inflate(getContext(), R.layout.loading_view, null);
				}
				MyViewSwitcher.this.removeViewAt(pos);
				viewSwitchContent.setTag(pos);
				MyViewSwitcher.this.addView(viewSwitchContent);
				MyViewSwitcher.this.reset();
				if(judgeFirstAndEnd){
					MyViewSwitcher.this.showNext();
					flag ++;
				}else{
					MyViewSwitcher.this.showPrevious();
					flag --;
				}
				if(flag==contentCount-1){
					judgeFirstAndEnd=end2First;
					setPreviousViewAnimation();
				}else if(flag==0){
					judgeFirstAndEnd=first2End;
					setNextViewAnimation();
				}
				handler.postDelayed(this, timeDistace);
			}
		}, timeDistace);
	}
/*	private LinearLayout getViewSwitcherContent(){
		LinearLayout linearLayout=new LinearLayout(context);
		View loadView=LayoutInflater.from(context).inflate(R.layout.loading_view, linearLayout);
		loadView.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		return linearLayout;
	}*/
	public void chageContent(View view,int index){
		view.setTag(this.getDisplayedChild());
		recordViews.put(index, view);
	}
	/**设置动画显示效果*/
	public void setPreviousViewAnimation(){
		this.setInAnimation(AnimationUtils.loadAnimation(context,R.anim.slide_in_right));
	}
	public void setNextViewAnimation(){
		this.setInAnimation(AnimationUtils.loadAnimation(context,android.R.anim.slide_in_left));
	}
}
